<?php
/**
 * Created by PhpStorm.
 * User: mac
 * Date: 2018/2/3
 * Time: 下午2:31
 */

function college_news()
{
    $data = $this->getParams('uid,c_name,c_url,template,content,last_title,least_title,edit_time,remark');
    $url = $data['c_url'];
    $curl = curl_init();//初始化一个curl会话；
    curl_setopt($curl,CURLOPT_URL,$url);//指定访问的url
    curl_setopt($curl,CURLOPT_RETURNTRANSFER,1);//获取的信息以文件流的方式返回
    curl_setopt($curl,CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.2; WOW64; rv:36.0) Gecko/20100101 Firefox/36.0');
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($curl, CURLOPT_TIMEOUT,10);   //只需要设置一个秒的数量就可以
    $content = curl_exec($curl);//执行curl;
    $encode = mb_detect_encoding($content, array("ASCII","UTF-8","GB2312","GBK",'BIG5'));
    if ($encode != "UTF-8"){
        $content = iconv($encode,"UTF-8",$content);
    }
    curl_close($curl); // 关闭CURL会话
    preg_match_all($data['template'],$content,$matches);
    if (count($matches)<2) {
        $data['status'] = 4; //结构变了
    } else {
        if (count(end($matches))<1) {
            $data['status'] = 4; //结构变了
        }else {
            $data['least_title'] = strip_tags(end($matches)[0]);
            if (strcmp($data['least_title'],$data['last_title'])==0) {
                $data['status'] = 2;//无变化
            } else if(empty($data['last_title'])){
                $data['status'] = 1;//未抓取
            } else {
                $data['status'] = 3;//有更新
            }
        }
    }
}

//一行一行的数据合并成pdf
function pdf(){
    ini_set("max_execution_time", "0");
    ini_set('memory_limit', '12800M');
    $c_id = '';
    $d_range = Db::query("select MAX(id) as max_did,MIN(id) as min_did from i_direction where college_id = $c_id");
    $max_id = $d_range[0]['max_did'];
    $min_id = $d_range[0]['min_did'];
    $directs = Db::table('i_direction')->join('i_subject','i_direction.id=i_subject.direction_id','left')
        ->where("i_direction.id>=$min_id and i_direction.id <= $max_id")->field('direction_id,dname,subject1,subject2,subject3,subject4,dremark')->select();
    //分组
    $groups = [];
    foreach ($directs as $direct) {
        $groups[$direct['direction_id']][] = $direct;
    }
    //单个direct合并
    $combineDirects = [];
    foreach ($groups as &$subject_list) {
        $statment = [];
        for ($i = 1;$i <= 4;$i++) {
            $sub_statment = "";
            for($j = 0;$j<count($subject_list);$j++) {
                $key = "subject".$i;
                $item = $subject_list[$j];
                if ($j == 0) {
                    $sub_statment = $item[$key];
                }else if(strcmp($sub_statment,$item[$key])!=0) {
                    $sub_statment .= '或'.$item[$key];
                }
            }
            $statment[] = $sub_statment;
        }
        $sentences =implode("和",$statment);
        $item = [];
        $item['direction_id'] = $subject_list[0]['direction_id'];
        $item['dname'] = $subject_list[0]['dname'];
        $item['subject'] = $sentences;
        $item['dremark'] = $subject_list[0]['dremark'];
        $item['row_span'] = 1;
        $combineDirects[] = $item;
    }
    //按照合并后的subject排序
    usort($combineDirects,function ($p1,$p2){
        return strcmp($p1['subject'],$p2['subject']);
    });
    //合并相同的subject
    $start = 0;
    $length = 1;
    for ($i = 0;$i<count($combineDirects)-1;$i++){
        if (strcmp($combineDirects[$i]['subject'],$combineDirects[$i+1]['subject'])==0){
            $length++;
            $combineDirects[$i+1]['row_span'] = 0;
        }else {
            $combineDirects[$start]['row_span'] = $length;
            $length = 1;
            $start = $i+1;
        }
    }
    //生成pdf
    $this->assign('directs',$combineDirects);
    $html = $this->fetch('collegePdf');
    $mpdf=new \mPDF('utf-8','A4', 0, '宋体', 0, 0);
    $mpdf -> useAdobeCJK = TRUE;
    $mpdf ->autoScriptToLang = true;
    $mpdf -> autoLangToFont = true;
    $mpdf->WriteHTML($html);
    $mpdf->Output('test.pdf');
    var_dump("生成pdf成功");
}